import { _decorator, Component, Node, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('HeartController')
export class HeartController extends Component {

    
    @property(Node)
    public Heart1 : Node;
    @property(Node)
    public Heart2 : Node;
    @property(Node)
    public Heart3 : Node;


    start() {

    }

    update(deltaTime: number) {
        
    }

    public setData(v:number)
    {
        this.Heart1.active = v >= 1;
        this.Heart2.active = v >= 2;
        this.Heart3.active = v>=3;
    }

    // 受伤害

    public hurt(afterVal : number , beforeVal : number)
    {
        let arr = [this.Heart1,this.Heart2,this.Heart3];
        for(let i = 0 ; i < arr.length ; i++)
        {
            if( i < afterVal )
            {
                arr[i].active = true;
            }
            else if( i >= afterVal && i < beforeVal )
            {
                arr[i].active = true;
                tween(arr[i]).stop();
                tween(arr[i])
                .delay(0.2).call(() =>
                {
                    arr[i].active = false;
                })
                .delay(0.2).call(() =>
                {
                    arr[i].active = true;
                })
                .delay(0.2).call(() =>
                {
                    arr[i].active = false;
                })
                .delay(0.2).call(() =>
                {
                    arr[i].active = true;
                })
                .delay(0.2).call(() =>
                {
                    arr[i].active = false;
                })
                .start();
            }
            else
            {
                arr[i].active = false;
            }
        }



    }
}

